import axios from 'axios'
import qs from 'qs'
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
// axios.defaults.headers.common['Authentication-Token'] = localStorage.getItem('token'); // 设置token
var domain = "/web"; //api域名
var traceId=localStorage.getItem('traceId');
//http request 拦截器
axios.interceptors.request.use((config) => {
	//添加traceId，方便没有登录时候识别用户
	//后续添加的拦截axios添加token到header中
    var token = localStorage.getItem('token');
    // 设置baseurl
    config.baseURL = domain;
	if(token) {
//		config.headers.token=token;
		// localStorage.removeItem('traceId');
		config.data.token = token
	}else if(traceId){
		config.data.trace_id=traceId;
	}
	if (config.method === 'post') {
	  config.data = qs.stringify(config.data);
	}
	return config;
  }, (error) => {
	console.log(error);
});

function post(url, params, callback, failcallback){
	var obj = initFn(params, callback, arguments[0]);
	var nparams = obj.nparams;
		callback = obj.callback;
	axios.post(url, nparams).then((response) => {
		var all = response.data;
		var data = response.data.data; //根据后端实际返回修改
		if(failcallback){
			if(all.code == '00000'){//success
				if(callback) callback(data, all);
			}else{
				failcallback(data, all);
			}
		}else{
//			if(all.code == '00000'){ //成功
				if(callback) callback(data, all);
//			}else{ //失败
//			}
		}
	}).catch((error)=>{
		console.log(error);
	});
}

function postXD(params, callback){
	var url = domain;
	var obj = initFn(params, callback, arguments[0]);
	var nparams = obj.nparams;
		callback = obj.callback;

	axios.post(url, nparams).then((response) => {
		var all = response.data;
		var data = response.data.data; //根据后端实际返回修改
		//根据后端实际返回修改
		if(all.code == '00000'){ //成功
			if(callback) callback(data, all);

		}else{ //失败
			console.log(all.code);
		}
		
	}).catch((error)=>{
		console.log(error);
	});
}
function get(params, callback){
	var url = domain;
	var obj = initFn(params, callback, arguments[0]); 
	var nparams = obj.nparams;
	callback = obj.callback;

	axios.get(url, {params: nparams}).then((response) => {
		var all = response.data; //返回所有数据
		var data = response.data.data; //根据后端实际返回修改
		//根据后端实际返回修改
		if(all.code == '00000'){ //成功
			if(callback) callback(data, all);
		}else{ //失败
			console.log(all.code);
		}
	}).catch((error)=>{
		console.log(error);	
	});
}
function del (url, params, callback){
    var url = domain;
	var obj = initFn(params, callback, arguments[0]); 
	var nparams = obj.nparams;
	callback = obj.callback;

	axios.delete(url, {params: nparams}).then((response) => {
		var all = response.data; //返回所有数据
		var data = response.data.data; //根据后端实际返回修改
		//根据后端实际返回修改
		if(all.code == '00000'){ //成功
			if(callback) callback(data, all);
		}else{ //失败
			console.log(all.code);
		}
	}).catch((error)=>{
		console.log(error);	
	});
}

function initFn(params, callback, argument){
	//如果没有参数
	if (typeof argument == "function"){
		callback = argument;
		params = {};
	}
	//对象合并,确定最终参数
	return {
		nparams: params,
		callback: callback
	} 
}
export{
	post, postXD,get,del
}